import {
	request
} from '@/api/request/index.js'
import share from '@/components/share.vue'
import cartModel from '@/api/cart/model.js'
import uParse from '@/components/richtext-parse/u-parse.vue'
import uFab from '@/components/uni-fab/uni-fab.vue'

export default {
	components: {
		share,
		uParse,
		uFab
	},
	data() {
		return {
			moreicon:this.$resBaseurl+"/act/mrhappy/share.png",
			isMpweixin: false,
			commoditypackid: -1,
			commodityid: -1,

			deliverRule: '',
			serviceStr: '',
			recommendCommdityList: [],
			packageNameList: [],
			packagelist:[],
			showCartBox:false,
			choosepackname:-1,
			commodityObject: [],
			loading: true,
			imageProp: {
				mode: 'aspectFill',
				padding: 5,
				lazyLoad: true
			},
			cartNum:cartModel.getCartNum(),
			shareConfig:{
				Title:'嗨焙鲜生',
				Contents:'嗨焙鲜生',
				ShareLink:'',
				ImgsrcMp:''
			}
		}
	},
	onShow() {
		// this.$setTitle("嗨焙鲜生·精选")
	},
	onLoad(options) {
		// #ifdef H5
		this.isMpweixin =false 
		this.initShare()
		// #endif
		// #ifdef MP-WEIXIN
		this.isMpweixin = true
		// #endif
		let commoditypackid = options.pid || -1;
		let commodityid = options.cid || -1;
		this.commoditypackid = commoditypackid;
		this.commodityid = commodityid;

		this.getDetails()
	},
	onPullDownRefresh() {
		this.getDetails()
	},
	//微信小程序分享
	onShareAppMessage: function(res) {
		if (res.from === 'button') {}
		return {
			title: this.shareConfig.Title,
			path: this.$currentlocation.getCurrentPageUrlWithArgs(),
			imageUrl: this.shareConfig.ImgsrcMp,
			success(res){
			}
		};
	},
	methods: {
		getDetails() {
			let that = this;
			if (this.commodityid == -1 || this.commoditypackid == -1) return uni.showModal({
				title: '提示',
				content: '产品ID错误，请返回重试'
			})
			uni.showLoading({
				mask: true,
				title: '正在加载模块...'
			});
			request.get({
					data: {
						c: 'SwiperController',
						m: 'GetProductDetails',
						city: '上海',
						commodityid: that.commodityid,
						commoditypackid: that.commoditypackid
					}
				})
				.then(res => {
					if (res.Status == "ok") {
						let result = res.Tag.resultDic;
						let Happy_Commodity = result["Happy_Commodity"] || [],
							Happy_CommodityPackages = result["Happy_CommodityPackages"] || [],
							serviceStr = result["serviceStr"] || '',
							delveryRule = result["delveryRule"] || '',
							swiperImgSrc = result["swiperImgSrc"] || '',
							recommendCommodityList = result["recommendCommodityList"] || [];
						that.serviceStr = serviceStr;
						that.deliverRule = delveryRule;
						
						let packageCurrent={}
						if(Happy_CommodityPackages.length>0){
							Happy_CommodityPackages.map((pitem)=>{
								if(pitem.Id == that.commoditypackid) {
									packageCurrent =pitem;
									that.choosepackname  = pitem.PackageName
								}
								pitem.Num=1;
							})
							
							that.packageNameList = Happy_CommodityPackages
						}
						if (Happy_Commodity.length > 0) {
							Happy_Commodity.map((hitem) => {
								let imageList=[]
								for (var a = 1; a <= 4; a++) {
									var topSwiper = that.$detailsListImgsrc.replace('{0}',hitem.CommodityName+a);
									imageList.push(topSwiper)
								}
								hitem.TopSwiperList = imageList;
								hitem.Package=packageCurrent;
								if(!hitem.Title || hitem.Title.trim().length==0) hitem.Title=''
							});
							that.commodityObject = Happy_Commodity
							
							that.shareConfig.Title = Happy_Commodity[0].Remarks || Happy_Commodity[0].CommodityName
							that.shareConfig.Contents = that.shareConfig.Title
						}
						
					} else {

					}
				})
				.catch(error => {
					console.error(error)
				});
		},

		resetProductlist(list = []) {
			let newlist = []
			list.map((item) => {
				item.Size = item.Size || "组";
				item.ImageSrc = "https://res.bestcake.com/m-images/ww/jd/兔小萌.png?v=9"
				item.Href = "/pages/detail/detail?cid=" + item.Id + "&pid=" + item.detailsId;
				if (item.DiscountPrice) item.DiscountPrice = parseFloat(item.DiscountPrice.toFixed(2));
				else item.DiscountPrice = 0;
				if (item.Contents && /(<|&lt;)script.*script(>|&gt;)/.test(item.Contents)) item.Contents = ""
				if (item.IsDefaultDisplay) newlist.push(item)
			});

			return newlist
		},
		goHref(href) {
			uni.switchTab({
				url: href
			})
		},
		openCartBox(item) {
			let result = this.packageNameList;
			this.showCartBox = true;
			let sizelist = []
			result.map((r) => {
				if (r.DiscountPrice) r.DiscountPrice = parseFloat(r.DiscountPrice.toFixed(2));
				else r.DiscountPrice = 0;
				sizelist.push(r.PackageName);
				r.PackageNameList = sizelist;
				r.detailsId = r.Id;
				r.Id = item.Id;
				r.CommodityName = item.CommodityName;
				r.Title = item.Title;
				r.DeliveryCity = item.DeliveryCity;
				r.DeliveryStore = item.DeliveryStore;
				r.IsFlash = item.IsFlash;
				r.Size = item.Unit;
				r.Remarks = item.Remarks
				r.ImageUrl = this.$detailsImgsrc.replace('{0}',item.CommodityName);
			});
			this.packagelist = result;

		},
		chooseCartBox: function(s) {
			this.choosepackname = s;
		},
		closeCartBox: function() {
			this.showCartBox = false;
		},
		updateCartNum: function(item, type) {
			item.Num = item.Num + type <= 1 ? 1 : item.Num + type;
		},
		addTocart(item) {
			let cartVals = {
				CommodityId: item.Id,
				CommodityName: item.CommodityName,
				CommodityDesc: item.Title || '',
				PackageId: item.detailsId,
				PackageSupplyNo: item.PackageSupplyNo,
				PackageName: item.PackageName,
				Price: item.UnitPrice,
				ZkPrice: item.DiscountPrice,
				DetailsList: [],
				ExpireTime: '',
				DeliveryCity: item.DeliveryCity,
				DeliveryStore: item.DeliveryStore,
				IsFlash: item.IsFlash || false,
				Num: item.Num,
				Size: item.Size
			};

			let r = cartModel.add(cartVals)
			if (r.length > 0){ 
				uni.showToast({
					title:'已加入购物车',
					icon: 'none'
				})
				this.cartNum=cartModel.getCartNum()
				this.closeCartBox()
			}
		},
		initShare() {
			let that =this
			this.$ShareSdk.Share({
				title: that.shareConfig.Title,
				link: that.$currentlocation.getCurrentPageUrlWithArgs(),
				imgUrl:that.$resBaseurl+'/m-images/share/index.jpg',
				success() {
		
				}
			})
		},
	}
}
